K8s YAML 项目

K8s YAML 服务

新增服务

项目中点击服务部分,进入服务管理页面。

创建服务

K8s YAML 服务的创建支持平台管理和代码仓库托管两种方式:

  • 平台管理:服务的 Kubenertes YAML 配置存储到 Zadig 系统中
  • 仓库托管:将服务用到的 YAML(包括服务配置使用的 Configmap)托管到代码仓库中,通过 Zadig 平台配置好文件的目录路径后,Zadig 从代码仓库同步,支持两种同步方式:
    • 手动同步:点击加载按钮,获取仓库中最新的配置
    • 自动同步:通过配置 Webhook(参阅 Webhook 配置),Zadig 监听分支上有代码改动,对应的服务配置会被自动同步。

平台管理

  • 点击新建按钮新建服务。

创建服务

  • 输入新的服务名称。

创建服务

  • 将服务 YAML 填入编辑器并保存。

创建服务

  • 更新环境,该服务会自动加入到选择的环境中。

更新环境

仓库托管

  • 选择仓库托管,选择代码仓库
  • 选择服务配置所在文件目录,加载服务

更新服务

平台管理

  • 修改服务 YAML 并保存。

修改服务

  • 选择相应环境进行更新。

更新环境中的服务

仓库托管

  • 提交服务配置变更到代码仓库

配置变更

  • 变更合并到主干分支后,通过 Webhook 的能力自动同步最新配置到 Zadig 系统。也可以在界面上手动同步服务配置,如下图所示。

服务手动配置同步

  • 在 Zadig 集成环境中,查看服务配置的变更,点击服务更新按钮执行更新操作

服务版本diff 服务更新

删除服务

  • 服务 模块中将服务配置删除。
  • 更新环境,将删除的服务从相应环境中移除。

删除环境

服务 YAML 样例

无状态服务

概念:服务运行的实例不会在本地存储需要持久化的数据,并且多个实例对于同一个请求响应的结果是完全一致的。可以参考这篇文章了解无状态服务的更多细节。

details

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: nginx-deployment
  5. spec:
  6. selector:
  7. matchLabels:
  8. app: nginx
  9. replicas: 2 # 2 个 Pod 实例
  10. template:
  11. metadata:
  12. labels:
  13. app: nginx
  14. spec:
  15. containers:
  16. - name: nginx
  17. image: nginx:1.14.2
  18. ports:
  19. - containerPort: 80

有状态服务

概念:服务的实例可以将一部分数据随时进行备份,并且在创建一个新的有状态服务时,可以通过备份恢复这些数据,以达到数据持久化的目的。可以参考这篇文章了解有状态服务的更多细节。

details ```yaml apiVersion: v1 kind: ConfigMap metadata: name: mysql labels: app: mysql data: master.cnf: |

  1. # Apply this config only on the master.
  2. [mysqld]
  3. log-bin

slave.cnf: |

  1. # Apply this config only on slaves.
  2. [mysqld]
  3. super-read-only

Headless service for stable DNS entries of StatefulSet members.

apiVersion: v1 kind: Service metadata: name: mysql labels: app: mysql spec: ports:

  • name: mysql port: 3306 clusterIP: None selector: app: mysql

Client service for connecting to any MySQL instance for reads.

For writes, you must instead connect to the master: mysql-0.mysql.

apiVersion: v1 kind: Service metadata: name: mysql-read labels: app: mysql spec: ports:

  • name: mysql port: 3306 selector: app: mysql

apiVersion: apps/v1beta1 kind: StatefulSet metadata: name: mysql spec: selector: matchLabels: app: mysql serviceName: mysql

1 master and 2 slave

replicas: 3 template: metadata: labels: app: mysql spec: initContainers:

  1. - name: init-mysql
  2. image: mysql:5.7
  3. command:
  4. - bash
  5. - "-c"
  6. - |
  7. set -ex
  8. # Generate mysql server-id from pod ordinal index.
  9. [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
  10. ordinal=${BASH_REMATCH[1]}
  11. echo [mysqld] > /mnt/conf.d/server-id.cnf
  12. # Add an offset to avoid reserved server-id=0 value.
  13. echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
  14. # Copy appropriate conf.d files from config-map to emptyDir.
  15. if [[ $ordinal -eq 0 ]]; then
  16. cp /mnt/config-map/master.cnf /mnt/conf.d/
  17. else
  18. cp /mnt/config-map/slave.cnf /mnt/conf.d/
  19. fi
  20. volumeMounts:
  21. - name: conf
  22. mountPath: /mnt/conf.d
  23. - name: config-map
  24. mountPath: /mnt/config-map
  25. - name: clone-mysql
  26. image: gcr.azk8s.cn/google-samples/xtrabackup:1.0
  27. command:
  28. - bash
  29. - "-c"
  30. - |
  31. set -ex
  32. # Skip the clone if data already exists.
  33. [[ -d /var/lib/mysql/mysql ]] && exit 0
  34. # Skip the clone on master (ordinal index 0).
  35. [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
  36. ordinal=${BASH_REMATCH[1]}
  37. [[ $ordinal -eq 0 ]] && exit 0
  38. # Clone data from previous peer.
  39. ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
  40. # Prepare the backup.
  41. xtrabackup --prepare --target-dir=/var/lib/mysql
  42. volumeMounts:
  43. - name: data
  44. mountPath: /var/lib/mysql
  45. subPath: mysql
  46. - name: conf
  47. mountPath: /etc/mysql/conf.d
  48. containers:
  49. - name: mysql
  50. image: mysql:5.7
  51. env:
  52. - name: MYSQL_ALLOW_EMPTY_PASSWORD
  53. value: "1"
  54. ports:
  55. - name: mysql
  56. containerPort: 3306
  57. volumeMounts:
  58. - name: data
  59. mountPath: /var/lib/mysql
  60. subPath: mysql
  61. - name: conf
  62. mountPath: /etc/mysql/conf.d
  63. resources:
  64. requests:
  65. cpu: 500m
  66. memory: 1Gi
  67. limits:
  68. cpu: 500m
  69. memory: 1Gi
  70. livenessProbe:
  71. exec:
  72. command: ["mysqladmin", "ping"]
  73. initialDelaySeconds: 30
  74. periodSeconds: 10
  75. timeoutSeconds: 5
  76. readinessProbe:
  77. exec:
  78. # Check we can execute queries over TCP (skip-networking is off).
  79. command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
  80. initialDelaySeconds: 5
  81. periodSeconds: 2
  82. timeoutSeconds: 1
  83. - name: xtrabackup
  84. image: gcr.azk8s.cn/google-samples/xtrabackup:1.0
  85. ports:
  86. - name: xtrabackup
  87. containerPort: 3307
  88. command:
  89. - bash
  90. - "-c"
  91. - |
  92. set -ex
  93. cd /var/lib/mysql
  1. # Determine binlog position of cloned data, if any.
  2. if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
  3. # XtraBackup already generated a partial "CHANGE MASTER TO" query
  4. # because we're cloning from an existing slave. (Need to remove the tailing semicolon!)
  5. cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
  6. # Ignore xtrabackup_binlog_info in this case (it's useless).
  7. rm -f xtrabackup_slave_info xtrabackup_binlog_info
  8. elif [[ -f xtrabackup_binlog_info ]]; then
  9. # We're cloning directly from master. Parse binlog position.
  10. [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
  11. rm -f xtrabackup_binlog_info xtrabackup_slave_info
  12. echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
  13. MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
  14. fi
  15. # Check if we need to complete a clone by starting replication.
  16. if [[ -f change_master_to.sql.in ]]; then
  17. echo "Waiting for mysqld to be ready (accepting connections)"
  18. until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done
  19. echo "Initializing replication from clone position"
  20. mysql -h 127.0.0.1 \
  21. -e "$(<change_master_to.sql.in), \
  22. MASTER_HOST='mysql-0.mysql', \
  23. MASTER_USER='root', \
  24. MASTER_PASSWORD='', \
  25. MASTER_CONNECT_RETRY=10; \
  26. START SLAVE;" || exit 1
  27. # In case of container restart, attempt this at-most-once.
  28. mv change_master_to.sql.in change_master_to.sql.orig
  29. fi
  30. # Start a server to send backups when requested by peers.
  31. exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
  32. "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
  33. volumeMounts:
  34. - name: data
  35. mountPath: /var/lib/mysql
  36. subPath: mysql
  37. - name: conf
  38. mountPath: /etc/mysql/conf.d
  39. resources:
  40. requests:
  41. cpu: 100m
  42. memory: 100Mi
  43. volumes:
  44. - name: conf
  45. emptyDir: {}
  46. - name: config-map
  47. configMap:
  48. name: mysql

volumeClaimTemplates:

  • metadata: name: data spec: accessModes: [“ReadWriteOnce”] resources:
    1. requests:
    2. storage: 10Gi
    ``` >